package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("分页查询接口")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询");
        return categoryService.page(categoryPageQueryDTO);
    }


    @ApiOperation("修改分类")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)//清除缓存
    public Result upadet(@RequestBody CategoryDTO categoryDTO) {
        return categoryService.update(categoryDTO);

    }

    @ApiOperation("启用禁用")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status, Long id) {
        return categoryService.updateStatus(status, id);
    }

    @ApiOperation("新增")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result addCteg(@RequestBody CategoryDTO categoryDTO) {
        return categoryService.addCteg(categoryDTO);
    }

    @ApiOperation("删除")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(Long id) {
        return categoryService.delete(id);
    }

    @ApiOperation("分类列表")
    @GetMapping("/list")
    public Result list(Integer type) {
        return categoryService.list(type);
    }

}
